package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class TestSelect {
    public static void main(String[] args) throws Exception{
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/tedu?useUnicode=true&characterEncoding=utf8&serverTimeZone=Asia/Shanghai";
        String user = "root";
        String pwd = "root";
        Connection conn = DriverManager.getConnection(url, user, pwd);
        System.out.println("连接成功~~");

        String sql="SELECT id,name FROM location";

        PreparedStatement ps = conn.prepareStatement(sql);

        ResultSet rs = ps.executeQuery();
        while (rs.next()) {//while循环一次,迭代一行
            //获取当前行中列名为id的列的值,类型是int
            int id = rs.getInt("id");
            //获取当前行中列名为name的列的值,类型是String
            String name = rs.getString("name");
            System.out.println(id + "\t" + name);//'\t'是Tab键
        }

        rs.close();
        ps.close();
        conn.close();

    }
}
